package com.tim.helper.class_parse;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.SingleMemberAnnotationExpr;
import org.junit.Test;

import java.io.File;
import java.util.List;

/**
 * https://blog.csdn.net/qq_42873640/article/details/126230801
 */       
public class ClassFileParse {
    @Test
    public void parse() throws Exception {
    String path =
        "E:\\gitlab\\xqxc\\plat\\plat-merchant\\plat-merchant-impl\\src\\main\\java\\com\\xqxc\\api\\impl\\plat\\merchant\\mq\\MerchantMessageListener.java" +
                "";
        CompilationUnit cu = JavaParser.parse(new File(path));
        List<Node> childNodes = cu.getChildNodes();
        for(Node node : childNodes.get(childNodes.size()-1).getChildNodes()){
            if(node instanceof FieldDeclaration){
                NodeList<AnnotationExpr> annotations = ((FieldDeclaration) node).getAnnotations();
                for(AnnotationExpr annoExpr : annotations){
                    System.out.println(annoExpr.getName());
                    if("Value".equals(annoExpr.getName().asString())){
                        System.out.println(((SingleMemberAnnotationExpr) annoExpr).getMemberValue());
                    }
                }
            }
        }
//        System.out.println(cu);
    }
}
